import {
    NativeAppEventEmitter,
    NativeModules,
    Platform
} from 'react-native'

const _permissionsModule = NativeModules.YuanXinSystemPermissionsManager;

const PermissionTypes = {
    ios: [
        'location',
        'camera',
        'microphone',
        'photo',
        'contacts',
        'event',
        'reminder',
        'bluetooth',
        'notification',
        'backgroundRefresh',
    ],
    android: [
        'location',
        'camera',
        'microphone',
        'contacts',
        'event',
        'photo',
        'storage',
    ]
};

export default class SystemPermissionsManager {

    constructor() {
        this.StatusUndetermined = 'undetermined'
        this.StatusDenied = 'denied'
        this.StatusAuthorized = 'authorized'
        this.StatusRestricted = 'restricted'

        this.getPermissionTypes().forEach(type => {
            let methodName = `${type}PermissionStatus`
            this[methodName] = p => {
                console.warn(`ReactNativePermissions: ${methodName} is depricated. Use getPermissionStatus('${type}') instead.`)
                return this.getPermissionStatus(p == 'reminder' ? p : type)
            }
        })
    }

	canOpenSettings(): Promise {
		return _permissionsModule.canOpenSettings()
	}

	openSettings(): Promise{
		return _permissionsModule.openSettings()
	}

    getPermissionTypes() {
        return PermissionTypes[Platform.OS];
    }

    getPermissionStatus(permission): Promise {
        if (this.getPermissionTypes().indexOf(permission) >= 0) {
            return _permissionsModule.getPermissionStatus(permission)
        } else {
            return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on ${Platform.OS}`)
        }
    }

    requestPermission(permission, type): Promise {
        let options;

        if (this.getPermissionTypes().indexOf(permission) === -1) {
            return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on ${Platform.OS}`)
        } else if (permission == 'backgroundRefresh') {
            return Promise.reject('ReactNativePermissions: You cannot request backgroundRefresh')
        } else if (permission == 'location') {
            options = type || 'whenInUse'
        } else if (permission == 'notification') {
            options = type || ['alert', 'badge', 'sound']
        }

        return _permissionsModule.requestPermission(permission, options)
    }

    checkMultiplePermissions(permissions): Promise {
        let i = permissions.length
        let that = this
        const obj = {}
        function processNext() {
            i--
            let p = permissions[i]

            if (!p) {
                return Promise.resolve(obj)
            }

            return that.getPermissionStatus(p)
                .then(res => {
                    obj[p] = res
                    return processNext()
                }).catch(e => {
                    console.warn(e)
                    return processNext()
                })
        }
        return processNext()
    }
}
